var of = require('OverFish'),
    env = require('OverFish').env,
    sys = require('sys'),
    fs = require('fs'),
    events = require('events');
    
exports.load = function () {
    
    fs.readdir(env.appDir, function (err, files) {
      
      files.forEach(function (module) {
        if (module.match(/Module$/))
        {
          fs.readdir(env.appDir + '/' + module, function (err, files) {
            //sys.puts(sys.inspect(f));
            files.forEach(function (type) {
              if ((type == 'backend') || (type == 'frontend')) {
                fs.readdir(env.appDir + '/' + module + '/' + type + '/controllers', function (err, files) {
                  files.forEach(function (controller) {
                    
                    if (controller.match(/Controller.js$/))
                    {
                      path = module + '/' + type + '/controllers/' + controller.replace('.js', '');
                      
                      env.controllers[path] = new events.EventEmitter();
                      
                      con = require(env.appDir + '/' + path);

                      con.actions.forEach(function (action) {
                        env.controllers[path].addListener(action['action'], action['callback']);
                        //sys.puts(sys.inspect(action['action']));
                      });
                      
                      
                    }
                  });
                  
                });
                
              }
            });
          });
          mod = require(env.appDir + '/' + module);
          
          env.modules.push({
            'name': mod.name,
            'backendMenu': mod.backendMenu
          });
          if (mod.backendMenu != undefined) {
            menu = new Array();
            mod.backendMenu.forEach(function (item) {
              
              menu.push(item);
            });
            env.backendMenu.push({
              'module': mod.name,
              'menu': menu
            });
          }
        }
      });
      
    });
    
    
  };